package SQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Deno2 {
    public static void main(String[] agrs) {

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            System.out.println("驱动加载成功!");//实现接口
            String url="jdbc:oracle:thin:@localhost:1521:xe";
            String user="hr";
            String pwd="123";
            Connection con= DriverManager.getConnection(url,user,pwd);//Connection 接口连接数据库
            System.out.println("连接成功！");
            String sql="create table student("+
                    "stu_name varchar2(20),"+
                    "age integer,"+
                    "addr varchar2(100)";
            String sql2="insert into student values('孙悟空',23,'昆明')";
            String sql3="insert into student values('孙悟空2',-23,'昆明')";
            String sql4="insert into student values('孙悟空3',23,'昆明')";
            System.out.println(sql2);
            Statement stm=con.createStatement();
            con.setAutoCommit(false);//不会自动提交
            try {
                stm.executeUpdate(sql2);//更新语句
                stm.executeUpdate(sql3);
                stm.executeUpdate(sql4);
            } catch (SQLException e) {
                System.err.println(e.getMessage());
                //con.rollback();
            }
            con.rollback();
            stm.execute(sql2);//查询语句
            stm.close();
            con.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e){
            System.err.println(e.getMessage());//输出错误的信息，err是输出红色，代表警告
        }
    }
}
